<?php

class JenerateComponent
{
	protected static $replace = array();
	protected static $name = "jenerator";
	protected static $FormalName = "Jenerator";
	protected static $lang_name = "JENERATOR";
	protected static $MultiView = "Accounts";
	protected static $multiview = "accounts";
	protected static $admin_path;
	protected static $admin;
	
	function __construct() {
		self::$admin = array (	'controllers' => 	array( 'jcontrolleradmin' => JPATH_ROOT . 'media/com_jenerator/templates/component/admin.controllers.multiview.php'),
													array( 'jcontrollerform' => JPATH_ROOT . 'media/com_jenerator/templates/component/admin.controllers.singleview.php' ),
								'helpers' => array(),
								'models' => array(),
								'views' => array());
		self::$replace = array(	'name' => self::$name, 
								'FormalName' => self::$FormalName,
								'LANGNAME' => self::$lang_name,
								'MultiView' => self::$MultiView,
								'multiview' => self::$multiview);
	}
	
	public static function test() {
		//$test = self::parseFile(self::$admin['controllers']['jcontrolleradmin']);
		$test = self::parseFile("admin.controllers.multiview.php.jen");
		self::writeFile($test);
	}
	
	private static function writeFile($file) {
		$name = self::$name . ".php";	
		echo "<h2>Create file $name now</h2>";
		$fp = fopen($name, "w");
		fwrite($fp, $file);
		fclose($fp);
	}
	
	private static function createDirectory($path) {
		return mkdir($path);
	}
	
	private static function parseFile($path) {
		ob_start();
		include($path);
		$buffer = ob_get_contents();
		ob_end_clean();
		var_dump (htmlentities($buffer));
		return self::processNames($buffer);
	}
	
	private static function processNames($file) {
		foreach (self::$replace as $key => $value) :
			$file = str_replace("{" . $key . "}", $value, $file);	
		endforeach;
		return $file;
	}
}
